package com.yu1996.gulimall.product.service.impl;

import com.yu1996.gulimall.product.entity.AttrEntity;
import com.yu1996.gulimall.product.service.AttrService;
import com.yu1996.gulimall.product.vo.BaseAttrs;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yu1996.common.utils.PageUtils;
import com.yu1996.common.utils.Query;

import com.yu1996.gulimall.product.dao.ProductAttrValueDao;
import com.yu1996.gulimall.product.entity.ProductAttrValueEntity;
import com.yu1996.gulimall.product.service.ProductAttrValueService;
import org.springframework.transaction.annotation.Transactional;

@Service("productAttrValueService")
public class ProductAttrValueServiceImpl
    extends ServiceImpl<ProductAttrValueDao, ProductAttrValueEntity>
    implements ProductAttrValueService {

  @Autowired private AttrService attrService;

  @Override
  public PageUtils queryPage(Map<String, Object> params) {
    IPage<ProductAttrValueEntity> page =
        this.page(
            new Query<ProductAttrValueEntity>().getPage(params),
            new QueryWrapper<ProductAttrValueEntity>());

    return new PageUtils(page);
  }

  /**
   * 批量保存spu的规格参数
   *
   * @param id
   * @param baseAttrs
   */
  @Override
  public void saveAttrs(Long id, List<BaseAttrs> baseAttrs) {
    if (baseAttrs != null && baseAttrs.size() != 0) {
      List<ProductAttrValueEntity> collect =
          baseAttrs.stream()
              .map(
                  baseAttr -> {
                    ProductAttrValueEntity entity = new ProductAttrValueEntity();
                    entity.setAttrId(baseAttr.getAttrId());
                    AttrEntity byId = attrService.getById(baseAttr.getAttrId());
                    entity.setAttrName(byId.getAttrName());
                    entity.setAttrValue(baseAttr.getAttrValues());
                    entity.setQuickShow(baseAttr.getShowDesc());
                    entity.setSpuId(id);
                    return entity;
                  })
              .collect(Collectors.toList());
      this.saveBatch(collect);
    }
  }
  /** 查询商品spu规格参数列表 */
  @Override
  public List<ProductAttrValueEntity> baseAttrListForSpu(Long spuId) {
    List<ProductAttrValueEntity> spu_id =
        this.list(new QueryWrapper<ProductAttrValueEntity>().eq("spu_id", spuId));
    return spu_id;
  }
  /** 修改商品spu规格参数列表 */
  @Transactional
  @Override
  public void updateSpuAttr(Long spuId, List<ProductAttrValueEntity> entities) {
    // 先删除之前的规格参数
    this.baseMapper.delete(new QueryWrapper<ProductAttrValueEntity>().eq("spu_id", spuId));
    // 新增规格参数并设置spuid
    List<ProductAttrValueEntity> collect =
        entities.stream()
            .map(
                entiti -> {
                  entiti.setSpuId(spuId);
                  return entiti;
                })
            .collect(Collectors.toList());
    this.saveBatch(collect);
  }
}
